package student.aistar.day15.syn;

public class SynLockExtendsDemo extends Thread {
    private static Object obj = new Object();
    @Override
    public void run() {
        test();
    }

    private void test() {
//        System.out.println(Thread.currentThread().getName() + "-begin...");
//        synchronized (this){//对象锁
//        synchronized (obj){//如果obj没有加static就是对象锁

        //类名.class - 获取类的实例
        //无论一个类被实例化多少次, 他在JVM得内存永远只有一个
//        synchronized (SynLockExtendsDemo.class){//类锁
        synchronized (obj){//如果obj是static - "内存只要一个"
            System.out.println(Thread.currentThread().getName() + ":0");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":1");

        }
    }

    public static void main(String[] args) {
        Thread t1 = new SynLockExtendsDemo();
        Thread t2 = new SynLockExtendsDemo();

        t1.start();
        t2.start();
    }
}
